const path = require("path");
const htmlWebpackPlugin = require("html-webpack-plugin");
const Purifycss = require("purifycss-webpack");
const glob = require("glob-all");
const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base.js");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const proConfig = {
  mode: "production",
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: "../",
            },
          },
          "css-loader",
          "postcss-loader",
          "less-loader",
        ],
      },
    ],
  },

  optimization: {
    usedExports: true,
  },
  plugins: [
    new htmlWebpackPlugin({
      template: "./src/index.html",
      filename: "index.html",
    }),

    new Purifycss({
      //摇树的范围 引入css的js模块  引入css的html
      paths: glob.sync([
        path.resolve(__dirname, "./dist/*.html"),
        path.resolve(__dirname, "./src/*.js"),
      ]),
    }),
    // new textWebpackPlugin({
    //   name: "my plugin",
    // }),
    new MiniCssExtractPlugin({
      filename: "css/[name]-[contenthash:6].css",
    }),
    // new webpack.HotModuleReplacementPlugin(),
  ],
};

module.exports = merge(baseConfig, proConfig);
